package com.mmlc.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.selector.OnWheelChangedListener;
import com.mmlc.selector.WheelView;
import com.mmlc.selector.adapters.ArrayWheelAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/7/16 0016.
 */
public class AddressSelectorView extends FrameLayout implements OnWheelChangedListener {
    private WheelView mViewProvince;
    private WheelView mViewCity;
    private WheelView mViewDistrict;
    protected String mCurrentProviceName;
    protected int mCurrentProvinceIndex;

    public AddressSelectorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(getContext(), R.layout.widget_address_selector, this);
        initView();
    }

    public AddressSelectorView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public AddressSelectorView(Context context) {
        this(context, null);
    }

    private void initView() {
        mViewProvince = (WheelView) findViewById(R.id.id_province);
        mViewCity = (WheelView) findViewById(R.id.id_city);
        mViewDistrict = (WheelView) findViewById(R.id.id_district);

        mViewProvince.addChangingListener(this);
        mViewCity.addChangingListener(this);
        mViewDistrict.addChangingListener(this);

        findViewById(R.id.tv_verify).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectedListener != null) {
                    selectedListener.onSelected(mCurrentProviceName, mCurrentProvinceIndex, mCurrentCityName, mCurrentCityIndex, mCurrentDistrictName, mCurrentDistrictIndex);
                }
            }
        });
    }

    OnSelectedListener selectedListener;

    public void setSelectedListener(OnSelectedListener selectedListener) {
        this.selectedListener = selectedListener;
    }

    public interface OnSelectedListener {
        void onSelected(String proviceName, int provinceIndex, String cityName, int cityIndex, String districtName, int districtIndex);

    }

    public List<String> mProvinceDatas;
    Map<String, List<String>> mCitisDatasMap;
    Map<String, List<String>> mDistrictDatasMap;

    public void setData(List<String> mProvinceDatas, Map<String, List<String>> mCitisDatasMap, Map<String, List<String>> mDistrictDatasMap) {
        this.mProvinceDatas = mProvinceDatas;
        this.mCitisDatasMap = mCitisDatasMap;
        this.mDistrictDatasMap = mDistrictDatasMap;
        if(mProvinceDatas==null||mProvinceDatas.size()==0){
            return;
        }

        mViewProvince.setViewAdapter(new ArrayWheelAdapter(getContext(), mProvinceDatas));
        // 设置可见条目数量
        mViewProvince.setVisibleItems(5);
        mViewCity.setVisibleItems(5);
        mViewDistrict.setVisibleItems(5);

        updateCities();
        updateAreas();
    }

    boolean update = false;

    int cIndex, dIndex;

    public void setSelectedIndex(int pIndex, int cIndex, int dIndex) {
        this.mCurrentProvinceIndex = pIndex;
        this.cIndex = cIndex;
        this.dIndex = dIndex;
        this.update = true;

        mViewProvince.setCurrentItem(mCurrentProvinceIndex);
        updateCities();
    }

    public String[] getResult() {
        String[] str = new String[]{mCurrentProviceName, mCurrentCityName, mCurrentDistrictName};
        return str;
    }

    private void updateCities() {

        int pCurrent = mViewProvince.getCurrentItem();
        if(pCurrent>=mProvinceDatas.size()){
            return;
        }
        mCurrentProvinceIndex = pCurrent;
        mCurrentProviceName = mProvinceDatas.get(pCurrent);
        List<String> cities = mCitisDatasMap.get(mCurrentProviceName);
        if (cities == null) {
            cities = new ArrayList<>();
        }
        mViewCity.setViewAdapter(new ArrayWheelAdapter(getContext(), cities));
        mCurrentCityIndex = 0;
        if (update) {
//            mViewCity.setCurrentItem(cIndex);
            mCurrentCityIndex = cIndex;
        }
        mViewCity.setCurrentItem(mCurrentCityIndex);

        updateAreas();
    }

    String mCurrentCityName;
    protected int mCurrentCityIndex;

    private void updateAreas() {
        int pCurrent = mViewCity.getCurrentItem();
        mCurrentCityIndex = pCurrent;
        mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName).get(pCurrent);
        List<String> areas = mDistrictDatasMap.get(mCurrentCityName);

        if (areas == null) {
            areas = new ArrayList<>();
        }
        mViewDistrict.setViewAdapter(new ArrayWheelAdapter(getContext(), areas));
        mCurrentDistrictIndex = 0;
        if (update) {
//            mViewDistrict.setCurrentItem(dIndex);
            mCurrentDistrictIndex = dIndex;
            update = false;
        }
        mViewDistrict.setCurrentItem(mCurrentDistrictIndex);
        if(mCurrentDistrictIndex<0 && areas.size()>0){
            mCurrentDistrictIndex = 0;
        }
        mCurrentDistrictName = areas.get(mCurrentDistrictIndex);
    }

    String mCurrentDistrictName;
    protected int mCurrentDistrictIndex;

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == mViewProvince && !update) {
            updateCities();
        } else if (wheel == mViewCity && !update) {
            updateAreas();
        } else if (wheel == mViewDistrict) {
            mCurrentDistrictIndex = newValue;
            mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName).get(newValue);
        }
    }
}
